package com.ruoyi.integral.service.impl;

import com.ruoyi.common.constant.IntegralConstants;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.integral.domain.Integral;
import com.ruoyi.integral.domain.IntegralApproval;
import com.ruoyi.integral.domain.IntegralJk;
import com.ruoyi.integral.domain.IntegralLog;
import com.ruoyi.integral.mapper.IntegralApprovalMapper;
import com.ruoyi.integral.mapper.IntegralJkMapper;
import com.ruoyi.integral.mapper.IntegralLogMapper;
import com.ruoyi.integral.mapper.IntegralMapper;
import com.ruoyi.integral.service.IIntegralJkService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 积分奖扣 服务层实现
 * 
 * @author sunli
 * @date 2018-10-25
 */
@Service
public class IntegralJkServiceImpl implements IIntegralJkService {
	@Autowired
	private IntegralJkMapper integralJkMapper;

	@Autowired
	private IntegralMapper integralMapper;

	@Autowired
	private SysUserMapper sysUserMapper;

	@Autowired
	private IntegralLogMapper integralLogMapper;

	@Autowired
	private IntegralApprovalMapper integralApprovalMapper;

	@Autowired
	private SysDeptMapper sysDeptMapper;

	@Autowired
	private SysUserPostMapper sysUserPostMapper;
	@Autowired
	private SysPostMapper sysPostMapper;


	/**
	 * 查询积分奖扣信息
	 *
	 * @param jkId 积分奖扣ID
	 * @return 积分奖扣信息
	 */
	@Override
	public IntegralJk selectIntegralJkById(Integer jkId) {
		return integralJkMapper.selectIntegralJkById(jkId);
	}

	/**
	 * 查询积分奖扣列表
	 *
	 * @param integralJk 积分奖扣信息
	 * @return 积分奖扣集合
	 */
	@Override
	public List<IntegralJk> selectIntegralJkList(IntegralJk integralJk) {
		return integralJkMapper.selectIntegralJkList(integralJk);
	}

	/**
	 * 新增积分奖扣
	 *
	 * @param integralJk 积分奖扣信息
	 * @return 结果
	 *//*
	@Override
	public int insertIntegralJk(IntegralJk integralJk, String[] jkNames) {
		integralJk.setJkNum("JKB" + DateUtils.dateTime());
		if (integralJk.getJkTime() != null) {
			integralJk.setJkTime(integralJk.getJkTime());
		}
		integralJk.setJkTime(new Date());
		*//** 根据部门id 和 员工姓名 查询员工*//*
		for (String jkName : jkNames) {
			Integral integral = null;
			if (integralJk.getDeptId() == null) {
				integral = getUserName(jkName);
				integralJk.setJkName(jkName);
			}
			else {
				SysUser sysUser = sysUserMapper.selectUserNameAndDeptId(integralJk.getDeptId(), jkName);
				integral = getUserName(sysUser.getUserName());
				integralJk.setJkName(jkName);
			}
			if (integralJk.getjIntegral() != null && integralJk.getjIntegral() > 0) {
				integralJk.setjIntegral(integralJk.getjIntegral());
				integral.setCountIntegral(integral.getCountIntegral() + integralJk.getjIntegral());
				if (integral.getAddIntegral() == null) {
					integral.setAddIntegral(0 + integralJk.getjIntegral());
				}
				else {
					integral.setAddIntegral(integral.getAddIntegral() + integralJk.getjIntegral());
				}
			}
			if (integralJk.getkIntegral() != null && integralJk.getkIntegral() > 0) {
				integralJk.setkIntegral(integralJk.getkIntegral());
				integral.setCountIntegral(integral.getCountIntegral() - integralJk.getkIntegral());
				if (integral.getDelIntegral() == null) {
					integral.setDelIntegral(0 + integralJk.getkIntegral());
				}
				else {
					integral.setDelIntegral(integral.getDelIntegral() + integralJk.getkIntegral());
				}
				integralJk.setkIntegral(integralJk.getkIntegral());
			}
			//修改积分
			int integraNum = integralMapper.updateIntegral(integral);
			//添加日志
			int log = insertIntegralLog(integralJk);
			//添加审批管理
			//int IntegralApproval = insertIntegralApproval(integralJk);
			//添加积分奖扣
			int integralNum = integralJkMapper.insertIntegralJk(integralJk);
		}
		return 1;
	}*/

	/**
	 * 添加审批积分奖扣信息
	 *
	 * @param
	 * @param integralJk
	 * @return 结果
	 */
	private int insertIntegralApproval(IntegralJk integralJk,SysUser user) {
		SysUserPost userPost = sysUserPostMapper.selectUserById(user.getUserId());

		IntegralApproval integralApproval = new IntegralApproval();
		integralApproval.setUserDept(integralJk.getDeptName());
		integralApproval.setUserPostId(userPost.getPostId().intValue());
		integralApproval.setUserPost(sysPostMapper.selectPostById(userPost.getPostId()).getPostName());
		integralApproval.setUserDeptId(user.getDeptId().intValue());
		integralApproval.setUserPhone(Long.parseLong(user.getPhonenumber()));
		integralApproval.setSpTime(integralJk.getJkTime());
		integralApproval.setStatus(1);
		integralApproval.setTypeId(integralJk.getTypeId());
		integralApproval.setApprovalContent(integralJk.getJkTitle());
		integralApproval.setApprovalNum(integralJk.getJkNum());
		integralApproval.setIntegralTypeId(integralJk.getTypeId());
		integralApproval.setUserName(integralJk.getJkName());
		integralApproval.setUserImg(integralJk.getJkImg());
		integralApproval.setSqIntegral(integralJk.getjIntegral());
		integralApproval.setkIntegral(integralJk.getkIntegral());
		return integralApprovalMapper.insertIntegralApproval(integralApproval);
	}

	/**
	 * 添加日志积分奖扣信息
	 *
	 * @param
	 * @param integralJk
	 * @return 结果
	 */
	private int insertIntegralLog(IntegralJk integralJk,SysUser user) {
		SysUserPost userPost = sysUserPostMapper.selectUserById(user.getUserId());

		IntegralLog log = new IntegralLog();
		log.setTypeId(integralJk.getTypeId());
		log.setUserPhone(Long.parseLong(user.getPhonenumber()));
		log.setUserPost(sysPostMapper.selectPostById(userPost.getPostId()).getPostName());
		log.setStatus(IntegralConstants.ZY_INTEGRAL);
		log.setGetTime(integralJk.getJkTime());
		log.setIntegralTitle(integralJk.getJkTitle());
		log.setBianIntegral(integralJk.getjIntegral());
		log.setTypeId(integralJk.getTypeId());
		log.setkIntegral(integralJk.getkIntegral());
		log.setUserName(integralJk.getJkName());
		log.setApprovalNum(integralJk.getJkNum());
		log.setUserDept(integralJk.getDeptName());
		log.setUserImg(integralJk.getJkImg());
		log.setIntegralContent(integralJk.getJkTitle());
		return integralLogMapper.insertIntegralLog(log);
	}

	/**
	 * 根据用户名查询积分
	 *
	 * @param userName 积分奖扣信息
	 * @return 结果
	 */
	private Integral getUserName(String userName) {

		return integralMapper.selectUserName(userName);
	}

	/**
	 * 修改积分奖扣
	 *
	 * @param integralJk 积分奖扣信息
	 * @return 结果
	 */
	@Override
	public int updateIntegralJk(IntegralJk integralJk) {
		return integralJkMapper.updateIntegralJk(integralJk);
	}

	/**
	 * 删除积分奖扣对象
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteIntegralJkByIds(String ids) {
		return integralJkMapper.deleteIntegralJkByIds(Convert.toStrArray(ids));
	}

	/**
	 * 	批量添加员工奖扣
	 */
	@Override
	public int insertIntegralUserList(IntegralJk integralJk) {
		 /** 所用的员工的 ids*/
	String [] ids =	integralJk.getIds().split(",");
		for (String id : ids) {
			IntegralJk jk = new IntegralJk();
			jk.setJkNum("JKB" + DateUtils.dateTime());
			jk.setJkTime(new Date());
			SysUser user = sysUserMapper.selectUserById(Long.parseLong(id));
			jk.setJkName(user.getUserName());
			jk.setJkPhone(user.getPhonenumber());
			jk.setJkImg(user.getAvatar());
			jk.setJkTitle(integralJk.getJkTitle());
			jk.setJkDescribe(integralJk.getJkDescribe());
			jk.setDeptId(user.getDeptId().intValue());
			jk.setDeptName(sysDeptMapper.selectDeptByName(user.getDeptId().intValue()).getDeptName());
			jk.setTypeId(integralJk.getTypeId());
			jk.setkIntegral(integralJk.getkIntegral());
			/** 总积分 */

			Integral integral =	integralMapper.selectByUserId(id);

			if(StringUtils.isNotNull(integralJk.getjIntegral())  && integralJk.getjIntegral() > 0){
				jk.setjIntegral(integralJk.getjIntegral());
				integral.setCountIntegral(integral.getCountIntegral() + integralJk.getjIntegral());

				if (integral.getAddIntegral() == null) {
					integral.setAddIntegral(0 + integralJk.getjIntegral());
				}
				else {
					integral.setAddIntegral(integral.getAddIntegral() + integralJk.getjIntegral());
				}
			}
			if(StringUtils.isNotNull(integralJk.getkIntegral())  && integralJk.getkIntegral() > 0){

				jk.setkIntegral(integralJk.getkIntegral());

				if (integral.getDelIntegral() == null) {
					integral.setDelIntegral(0 + integralJk.getkIntegral());
				}
				else {
					integral.setDelIntegral(integral.getDelIntegral() + integralJk.getkIntegral());
				}
				integral.setCountIntegral(integral.getCountIntegral() - integralJk.getkIntegral());
			}

			//修改积分
			int integraNum = integralMapper.updateIntegral(integral);
			//添加日志
			int log = insertIntegralLog(jk,user);
			//添加审批管理
			int IntegralApproval = insertIntegralApproval(jk,user);
			//添加积分奖扣
			int integralNum = integralJkMapper.insertIntegralJk(jk);

		}
		return 1;
	}

	/**
	 * 查询部门分组人名
	 *
	 * @return 结果
	 */
	/*@Override
	public List<Map<String, String>> jkNameGroup() {
		Map<String, Object> map = new HashMap<>();

		List<Map<String, String>> list = integralJkMapper.selectDeptAndUserName();
		for (Map<String, String> st : list) {
			//对象
			JSONObject str = JSONObject.fromObject(st);

			if (str.has("deptName")) {
				JSONArray tr = str.getJSONArray("deptName");
				System.out.println(tr);

			}
		}

			return null;
		}*/
	}



		/**
		 * 批量新增积分奖扣
		 *
		 *  @param integralJk 积分奖扣信息
		 *  @param jkNames
		 *  @return 结果
		 */
	/*@Override
	public int insertAllIntegralJk(IntegralJk integralJk, String[] jkNames) {



		return 0;
	}*/


